package app.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

public class FileDownloader {
	
	public static void main(String[] args) throws Exception {
		new FileDownloader().file("http://open.189.cn/statics/v5/images/brand.png", "e:");
	}
	
	public File file(String url,String path) throws Exception {
		URL tar=new URL(url);
		URLConnection connection = tar.openConnection();
		InputStream imageInput = connection.getInputStream();
		File file=new File(path+"/"+System.currentTimeMillis()+url.substring(url.lastIndexOf("."), url.length()));
		if (!file.exists()) {
			file.createNewFile();
		}
		FileOutputStream fileOutputStream=new FileOutputStream(file);
		byte[] buff=new byte[100];
		int len=0;
		while((len=imageInput.read(buff))>0) {
			fileOutputStream.write(buff, 0, len);
		}
		fileOutputStream.flush();
		fileOutputStream.close();
		return file;
	}
}
